//
//  MPBidTableCell.m
//  M800Project
//
//  Created by 姚宗超 on 14/11/6.
//  Copyright (c) 2014年 姚宗超. All rights reserved.
//

#import "MPBidTableCell.h"
#import "MPBidObject.h"
#import "UIImage+Common.h"
#import "MPProgressView.h"

@interface MPBidTableCell ()
@property (nonatomic, strong)UILabel* titleLabel;
@property (nonatomic, strong)UILabel* rateLabel;
@property (nonatomic, strong)UILabel* rateDesLabel;
@property (nonatomic, strong)UILabel* durationLabel;
@property (nonatomic, strong)UILabel* durationDesLabel;
@property (nonatomic, strong)UIImageView* accessoryImageView;
@property (nonatomic, strong)UIImageView* topSeperatorLineImageView;
@property (nonatomic, strong)UIImageView* bottomSeperatorLineImageView;
@property (nonatomic, strong)UIImageView* bgImageView;

@property (nonatomic, strong)MPProgressView* progressView;
@end

@implementation MPBidTableCell

+ (CGFloat)rowHeightForObject:(id)object
{
    return 86.0f;
}

+ (BOOL)shouldUseAutoLayout
{
    return YES;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryNone;
        
        [self.contentView addSubview:self.topSeperatorLineImageView];
        [self.contentView addSubview:self.bgImageView];
        [self.contentView addSubview:self.bottomSeperatorLineImageView];
        [self.bgImageView addSubview:self.titleLabel];
        [self.bgImageView addSubview:self.rateLabel];
        [self.bgImageView addSubview:self.rateDesLabel];
        [self.bgImageView addSubview:self.durationLabel];
        [self.bgImageView addSubview:self.durationDesLabel];
        [self.bgImageView addSubview:self.progressView];
        [self.bgImageView addSubview:self.accessoryImageView];

        [self setupConstraints];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)prepareForReuse
{
    [super prepareForReuse];
}

- (void)setupConstraints
{
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.trailing.mas_equalTo(self);
    }];
    
    [self.topSeperatorLineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.mas_equalTo(self.contentView);
        make.height.mas_equalTo(@1.0f);
    }];
    
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.topSeperatorLineImageView.mas_bottom);
    }];
    
    [self.bottomSeperatorLineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.bgImageView.mas_bottom);
        make.bottom.mas_equalTo(self.contentView).with.offset(-10.0f);
        make.height.mas_equalTo(@1.0f);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgImageView).with.offset(15.0f);
        make.leading.mas_equalTo(self.bgImageView).with.offset(10.0f);
        make.width.mas_equalTo(@(70.0f));
    }];
    
    [self.rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.titleLabel);
        make.leading.mas_equalTo(self.titleLabel.mas_trailing).with.offset(10.0f);
        make.width.mas_equalTo(@(70.0f));
    }];
    
    [self.rateDesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.rateLabel);
        make.top.mas_equalTo(self.rateLabel.mas_bottom).with.offset(10.0f);
        make.width.mas_equalTo(self.rateLabel);
        make.bottom.mas_equalTo(self.bgImageView).with.offset(-15.0f);
    }];
    
    
    [self.durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.rateLabel.mas_trailing).with.offset(10.0f);
        make.centerY.mas_equalTo(self.titleLabel);
        make.width.mas_equalTo(@(70.0f));
    }];
    
    [self.durationDesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.durationLabel);
        make.centerY.mas_equalTo(self.rateDesLabel);
        make.width.mas_equalTo(self.durationLabel);
        make.height.mas_equalTo(self.rateDesLabel);
    }];
    
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bgImageView);
        make.leading.mas_equalTo(self.durationLabel.mas_trailing).with.offset(10.0f);
        make.width.height.mas_equalTo(@40.0f);
    }];
    
    [self.accessoryImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.bgImageView);
        make.leading.mas_equalTo(self.progressView.mas_trailing).with.offset(10.0f);
        make.trailing.mas_equalTo(self.bgImageView);
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)setObject:(id)object
{
    [super setObject:object];
    
    if (object == nil)
    {
        return ;
    }
    else
    {
        if ([object isKindOfClass:[MPBidObject class]]) {
            MPBidObject* bid = (MPBidObject *)object;
            if (![NSString stringIsEmpty:bid.bidPlatformName]) {
                self.titleLabel.text = bid.bidPlatformName;
            }
            
            self.rateLabel.text = bid.yearRate;
            self.durationLabel.text = bid.duration;
            [self.progressView setProgress:bid.bidProgress animated:YES];
        }
    }
}


#pragma mark - property
- (UIImageView *)topSeperatorLineImageView
{
    if (!_topSeperatorLineImageView) {
        _topSeperatorLineImageView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor colorWithHexString:kCellBorderColor]]];
    }
    return _topSeperatorLineImageView;
}

- (UIImageView *)bottomSeperatorLineImageView
{
    if (!_bottomSeperatorLineImageView) {
        _bottomSeperatorLineImageView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor colorWithHexString:kCellBorderColor]]];
    }
    return _bottomSeperatorLineImageView;
}

- (UIImageView *)bgImageView
{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor whiteColor]]];
    }
    return _bgImageView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.textColor = [UIColor colorWithHexString:kContentTextColor];
        _titleLabel.font = [UIFont systemFontOfSize:kContentFontSize];
    }
    return _titleLabel;
}

- (MPProgressView *)progressView
{
    if (!_progressView) {
        _progressView = [[MPProgressView alloc] init];
    }
    return _progressView;
}

- (UILabel *)rateLabel
{
    if (!_rateLabel) {
        _rateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _rateLabel.textColor = [UIColor colorWithHexString:kHighlightTextColor];
        _rateLabel.textAlignment = NSTextAlignmentCenter;
        _rateLabel.font = [UIFont systemFontOfSize:kHighlightFontSize];
    }
    return _rateLabel;
}

- (UILabel *)rateDesLabel
{
    if (!_rateDesLabel) {
        _rateDesLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _rateDesLabel.textColor = [UIColor colorWithHexString:kTextUnSelectedGrayColor];
        _rateDesLabel.font = [UIFont systemFontOfSize:kDescribeFontSize];
        _rateDesLabel.textAlignment = NSTextAlignmentCenter;
        _rateDesLabel.text = @"年化收益";
    }
    return _rateDesLabel;
}

- (UILabel *)durationLabel
{
    if (!_durationLabel) {
        _durationLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _durationLabel.textColor = [UIColor colorWithHexString:kContentTextColor];
        _durationLabel.font = [UIFont systemFontOfSize:kContentFontSize];
        _durationLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _durationLabel;
}

- (UILabel *)durationDesLabel
{
    if (!_durationDesLabel) {
        _durationDesLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _durationDesLabel.textColor = [UIColor colorWithHexString:kTextUnSelectedGrayColor];
        _durationDesLabel.font = [UIFont systemFontOfSize:kDescribeFontSize];
        _durationDesLabel.textAlignment = NSTextAlignmentCenter;
        _durationDesLabel.text = @"投资期限";
    }
    return _durationDesLabel;
}

- (UIImageView *)accessoryImageView
{
    if (!_accessoryImageView) {
        _accessoryImageView = [[UIImageView alloc] initWithImage:[UIImage imageForKey:@"accessory_icon"]];
    }
    return _accessoryImageView;
}


@end
